package cn.jekun.shop.web.action;

import javax.annotation.Resource;

import net.sf.json.JSONObject;
import cn.jekun.component.base.bean.ErrorMsg;
import cn.jekun.component.base.exceptions.BusinessException;
import cn.jekun.shop.common.service.ShopBaseAction;
import cn.jekun.shop.web.service.LocationService;
/**
 * 我的位置
 * @author twj
 *
 */
@SuppressWarnings("serial")
public class LocationAction extends ShopBaseAction{
	@Resource(name = LocationService.BEAN_NAME)
	protected LocationService locationService;
	/**
	 * 删除我的位置
	 * @return
	 */
	public String delLocation(){
		JSONObject jsonObject = null;
		//验证是否登录
		if(!isLogin()){
			return timeout();
		}
		try{
		String locationId = this.request.getParameter("locationId");
		jsonObject = locationService.del(locationId);
	} catch (BusinessException e) {
		// TODO: handle exception
		jsonObject = JSONObject.fromObject(e.getErrorMsg());
	} catch (Exception e) {
		jsonObject = JSONObject.fromObject(new ErrorMsg(e.getMessage()));
		e.printStackTrace();
	}
		return this.writeJson(jsonObject);
	}
	/**
	 * 保存我的位置
	 * @return
	 */
	public String saveLocation(){
		JSONObject jsonObject = null;
		//验证是否登录
		if(!isLogin()){
			return timeout();
		}
		try{
		String lng = this.request.getParameter("lng");//经度
		String lat = this.request.getParameter("lat");//纬度
		String memo = this.request.getParameter("memo");//位置描述
		String userId = this.request.getParameter("userId");//用户代码
		String def = this.request.getParameter("def");
		jsonObject = locationService.save(lng,lat,memo,userId,def);
	
		this.logService.addMallLog(this.getUserId(), 
				this.getBrowser(), this.getIp(), "保存我的位置成功,来源:web,用户代码:"+this.getUserId());//记录操作日志

	} catch (BusinessException e) {
		// TODO: handle exception
		jsonObject = JSONObject.fromObject(e.getErrorMsg());
			this.logService.addMallLog(this.getUserId(), 
				this.getBrowser(), this.getIp(), "保存我的位置失败,原因:"+jsonObject.get("message")+",来源:web,用户代码:"+this.getUserId());//记录操作日志

	
	} catch (Exception e) {
		jsonObject = JSONObject.fromObject(new ErrorMsg(e.getMessage()));
	
		e.printStackTrace();
	}
		return this.writeJson(jsonObject);
	}
	/**
	 * 我的位置列表
	 * @return
	 */
	public String locationList(){
		JSONObject jsonObject = null;
		//验证是否登录
		if(!isLogin()){
			return timeout();
		}							
		try{
		int pageSize = Integer.parseInt(this.request.getParameter("pageSize"));
		int currentPage = Integer.parseInt(this.request.getParameter("currentPage"));
		String userId = this.request.getParameter("userId");
		jsonObject = locationService.query(pageSize, currentPage,userId);
		} catch (BusinessException e) {
			// TODO: handle exception
			jsonObject = JSONObject.fromObject(e.getErrorMsg());
		} catch (Exception e) {
			jsonObject = JSONObject.fromObject(new ErrorMsg(e.getMessage()));
			e.printStackTrace();
		}
		return this.writeJson(jsonObject);
	}
	/**
	 * 加载我的位置信息
	 * @return
	 */
	public String loadLocation(){
		JSONObject jsonObject = null;
		//验证是否登录
		if(!isLogin()){
			return timeout();
		}
		try{
		String locationId = this.request.getParameter("locationId");//location编码
		jsonObject = locationService.load(locationId);
		} catch (BusinessException e) {
			// TODO: handle exception
			jsonObject = JSONObject.fromObject(e.getErrorMsg());
		} catch (Exception e) {
			jsonObject = JSONObject.fromObject(new ErrorMsg(e.getMessage()));
			e.printStackTrace();
		}
		return this.writeJson(jsonObject);
	}
	public String updateLocation(){
		JSONObject jsonObject = null;
		//验证是否登录
		if(!isLogin()){
			return timeout();
		}
		try{
		String locationId = this.request.getParameter("locationId");
		/*String lng = this.request.getParameter("lng");
		String lat = this.request.getParameter("lat");
		String memo = this.request.getParameter("memo");*/
		String def = this.request.getParameter("def");
		String userId = this.getUserId();
		jsonObject = locationService.update(locationId,def,userId);
		this.logService.addMallLog(this.getUserId(), 
				this.getBrowser(), this.getIp(), "更新我的位置成功,来源:web,用户代码:"+this.getUserId());//记录操作日志

		} catch (BusinessException e) {
			// TODO: handle exception
			jsonObject = JSONObject.fromObject(e.getErrorMsg());
			this.logService.addMallLog(this.getUserId(), 
					this.getBrowser(), this.getIp(), "更新我的位置失败,原因:"+jsonObject.get("message")+",来源:web,用户代码:"+this.getUserId());//记录操作日志

		} catch (Exception e) {
			jsonObject = JSONObject.fromObject(new ErrorMsg(e.getMessage()));
			e.printStackTrace();
		
		}
		return this.writeJson(jsonObject);
	}

}
